package com.fairytask
{
	import com.fairytask.data.Modulator;
	import com.fairytask.event.LogicEvent;
	import com.fairytask.logic.BattleLogic;
	import com.fairytask.logic.LoginLogic;
	import com.fairytask.logic.SceneLogic;
	import com.fairytask.managers.ResManager;
	import com.fairytask.net.NetSocket;
	import com.fairytask.resource.StaticIni;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.smartfoxserver.v2.core.SFSEvent;
	
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;

	/**
	 * 游戏场景 
	 * @author game
	 * 
	 */	
	public class MMoGame
	{
		private static var _instance	:MMoGame;
		
		//设置显示层次
		private var model:Modulator;
		//加载
		private var loadMax	:LoaderMax;
		
		public static function get instance():MMoGame
		{
			if(null==_instance)
			{
				_instance	=	new MMoGame();
			}
			return _instance;
		}

		
		public function MMoGame()
		{
			super();
			loadMax	=	new LoaderMax({onProgress:onProgress,onComplete:onLoaded});
			
			model=Modulator.instance;
			//先加载服务器配置
			StaticIni.initByPath("config/serverConfig.ini");
			model.addEventListener(LogicEvent.SERVERINIT,onServerConfig);
		}
		
		/**
		 * 加载进程 
		 * @param event
		 * 
		 */
		private function onProgress(event:LoaderEvent):void
		{
			
		}
		
		protected function onServerConfig(event:Event):void
		{
			model.addEventListener(SFSEvent.CONNECTION,onSocketReady);
			var context:LoaderContext=new LoaderContext();
			if (Security.sandboxType == Security.REMOTE)
			{
				context.securityDomain=SecurityDomain.currentDomain;
			}
			context.applicationDomain=ApplicationDomain.currentDomain;
			//先加载基础配置文件
			loadMax.append(new SWFLoader("config/ConfigData.swf",{name:"config"}));
			loadMax.append(new SWFLoader("res/swf/newUI.swf",{name:"skins",context:context}));
			loadMax.append(new SWFLoader("res/swf/res.swf",{name:"res",context:context}));
			loadMax.load();
		}
		
		/**
		 * 资源加载完毕 
		 * @param event
		 * 
		 */
		protected function onLoaded(event:LoaderEvent):void
		{
			StaticIni.initStaticFromObj(loadMax.getContent("config"));
			//Debug.trace("=================",StaticIni.getIniObj("20001"));
			//addChild(AssetManager.instance.getMovieClip("button_002"));
			ResManager.setOutRes("skins",loadMax.getContent("skins"));
			ResManager.setOutRes("res",loadMax.getContent("res"));
			initLogic();
		}
		
		/**
		 * 初始化逻辑 
		 * 
		 */
		private function initLogic():void
		{
			BattleLogic.instance;
			SceneLogic.instance;
			LoginLogic.instance;
			//测试用
			/*var bData	:BattleData	=	new BattleData();
			bData.bid	=	"100001";
			bData.playerUnitType	=	30011;
			bData.numUnits	=	100;
			model.dispatchEvent(new BattleEvent(BattleEvent.ENTER_BATTLE,bData));*/
			
			//链接服务器
			NetSocket.instance.connect();
		}
		
		/**
		 * 服务器链接成功 
		 * @param evt
		 * 
		 */
		private function onSocketReady(evt:SFSEvent):void
		{
			//初始化UI
			model.dispatchEvent(new LogicEvent(LogicEvent.GETSERVER_INFO));
		}
	}
}